package com.LeetCode.Math;

import org.junit.Test;

/**
 * 反转整数
 */
public class ReverseInt {
    public int reverse(int x) {
        boolean isNegative = x < 0;
        String str = String.valueOf(x);
        int boom = Integer.MAX_VALUE / 10;
        if(isNegative) str = str.substring(1);
        String rev_str = new StringBuilder(str).reverse().toString();
        int res = 0;
        for(int i = 0; i<rev_str.length(); i++){
            if(res > boom || res == boom && rev_str.charAt(i) > '7'){
                return 0;
            }
            res *= 10;
            res += rev_str.charAt(i) - '0';
        }
        return isNegative ? -res : res;
    }

    @Test
    public void test(){
        System.out.println(reverse(-123));
    }
}
